package com.example.audiostreaming.utils;

import com.example.audiostreaming.base.BaseFragment;
import com.example.audiostreaming.fragments.FragmentHistory;
import com.example.audiostreaming.fragments.FragmentMine;
import com.example.audiostreaming.fragments.FragmentRecommend;
import com.example.audiostreaming.fragments.FragmentSubscription;

import java.util.HashMap;
import java.util.Map;

/**
 * Fragment片段工具类
 * 1.创建Fragment片段
 * 2.缓存已经创建的Fragment片段
 */
public class UtilFragmentCreator {
    private static Map<Integer, BaseFragment> sFragmentHashMap = new HashMap<>();//Fragment片段集合列表缓存

    public static BaseFragment getFragment(int index) {
        BaseFragment baseFragment = sFragmentHashMap.get(index);//从Fragment片段集合列表缓存中获取对应index索引的Fragment片段
        //Fragment片段内容不为空，则返回Fragment片段
        if (baseFragment != null) {
            return baseFragment;
        }

        //Fragment片段内容为空，根据index索引创建对应的Fragment片段
        switch (index) {
            case UtilConstants.INDEX_RECOMMEND:
                baseFragment = new FragmentRecommend();
                break;
            case UtilConstants.INDEX_SUBSCRIPTION:
                baseFragment = new FragmentSubscription();
                break;
            case UtilConstants.INDEX_HISTORY:
                baseFragment = new FragmentHistory();
                break;
//            case UtilConstants.INDEX_MINE:
//                baseFragment = new FragmentMine();
//                break;
        }
        //将创建的Fragment片段放入Fragment片段集合列表缓存，并返回Fragment片段
        sFragmentHashMap.put(index, baseFragment);
        return baseFragment;
    }
}
